//
// xmldefn.cpp
// libprotea
// Tools Library for the Protea Project / XML Protea DOM definition
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
// 
// Please send correspondence to:
//
// theproteaproject@gmail.com
//

// xmldefn.cpp
//
// Functions for the Protea DOM classes

#include "../defines.h"
#include "xmldefn.h"

// XML Property
//////////////////////////////////////////////////////////////////////////////
XCXML_Property::XCXML_Property()
{
	Name = NULL;
	Value = NULL;
	next = NULL;
};

XCXML_Property::~XCXML_Property()
{
	if (Name) delete[] Name;
	if (Value) delete[] Value;
};

// XML Script
//////////////////////////////////////////////////////////////////////////////
XCXML_Script::XCXML_Script()
{
	Type = NULL;
	Event = NULL;
	Value = NULL;
	next = NULL;
};

XCXML_Script::~XCXML_Script()
{
	if (Type) delete[] Type;
	if (Event) delete[] Event;
	if (Value) delete[] Value;
};

// XML Trigger
//////////////////////////////////////////////////////////////////////////////
XCXML_Trigger::XCXML_Trigger()
{
	Key = NULL;
	Type = NULL;
	Value = NULL;
	next = NULL;
};

XCXML_Trigger::~XCXML_Trigger()
{
	if (Key) delete[] Key;
	if (Type) delete[] Type;
	if (Value) delete[] Value;
};

// XML Object
//////////////////////////////////////////////////////////////////////////////
XCXML_Object::XCXML_Object()
{
	Number = NULL;
	Name = NULL;
	PropertyList = NULL;
	ScriptList = NULL;
	next = NULL;
};

XCXML_Object::~XCXML_Object()
{
	if (Number) delete[] Number;
	if (Name) delete[] Name;
	if (PropertyList)
	{
		XCXML_Property* ptemp;
		while (PropertyList)
		{
			ptemp = PropertyList;
			PropertyList = PropertyList->next;
			delete ptemp;
		};
	};
	if (ScriptList)
	{
		XCXML_Script* stemp;
		while (ScriptList)
		{
			stemp = ScriptList;
			ScriptList = ScriptList->next;
			delete stemp;
		};
	};
};

XCXML_Property* XCXML_Object::AddProperty()
{
	XCXML_Property* temp = PropertyList;
	XCXML_Property* newprop = new XCXML_Property();
	if (temp)
	{
		while (temp->next)
			temp = temp->next;
		temp->next = newprop;		
	}
	else
		PropertyList = newprop;
	return newprop;
};

XCXML_Property* XCXML_Object::GetProperty( char* Name )
{
	assert( Name );
	XCXML_Property* temp = PropertyList;
	while (temp)
	{
		if ( !strcmp( temp->Name, Name ) )
			return temp;
	};
	return NULL;
};

XCXML_Script* XCXML_Object::AddScript()
{
	XCXML_Script* temp = ScriptList;
	XCXML_Script* newscript = new XCXML_Script();
	if ( temp )
	{
		while ( temp->next )
			temp = temp->next;
		temp->next = newscript;
	}
	else
		ScriptList = newscript;
	return newscript;
};

XCXML_Script* XCXML_Object::GetScript( char* Event )
{
	assert( Event );
	XCXML_Script* temp = ScriptList;
	while (temp)
	{
		if ( !strcmp( temp->Event, Event ) )
			return temp;
	};
	return NULL;
};

// XML Form
//////////////////////////////////////////////////////////////////////////////
XCXML_Form::XCXML_Form()
{
	Number = NULL;
	Name = NULL;
	PropertyList = NULL;
	ScriptList = NULL;
	TriggerList = NULL;
	ObjectList = NULL;
	next = NULL;
};

XCXML_Form::~XCXML_Form()
{
	if (Number) delete[] Number;
	if (Name) delete[] Name;
	if (PropertyList)
	{
		XCXML_Property* ptemp;
		while (PropertyList)
		{
			ptemp = PropertyList;
			PropertyList = PropertyList->next;
			delete ptemp;
		};
	};
	if (ScriptList)
	{
		XCXML_Script* stemp;
		while (ScriptList)
		{
			stemp = ScriptList;
			ScriptList = ScriptList->next;
			delete stemp;
		};
	};
	if (TriggerList)
	{
		XCXML_Trigger* ttemp;
		while (TriggerList)
		{
			ttemp = TriggerList;
			TriggerList = TriggerList->next;
			delete ttemp;
		};
	};
	if (ObjectList)
	{
		XCXML_Object* otemp;
		while (ObjectList)
		{
			otemp = ObjectList;
			ObjectList = ObjectList->next;
			delete otemp;
		};
	};
};

XCXML_Property* XCXML_Form::AddProperty()
{
	XCXML_Property* temp = PropertyList;
	XCXML_Property* newprop = new XCXML_Property();
	if ( temp )
	{
		while ( temp->next )
			temp = temp->next;
		temp->next = newprop;
	}
	else
		PropertyList = newprop;
	return newprop;
};


XCXML_Property* XCXML_Form::GetProperty( char* Name )
{
	assert( Name );
	XCXML_Property* temp = PropertyList;
	while (temp)
	{
		if ( !strcmp( temp->Name, Name ) )
			return temp;
	};
	return NULL;
};

XCXML_Script* XCXML_Form::AddScript()
{
	XCXML_Script* temp = ScriptList;
	XCXML_Script* newscript = new XCXML_Script();
	if ( temp )
	{
		while ( temp->next )
			temp = temp->next;
		temp->next = newscript;
	}
	else
		ScriptList = newscript;
	return newscript;
};

XCXML_Script* XCXML_Form::GetScript( char* Type, char* Event )
{
	assert( Type );
	assert( Event );
	XCXML_Script* temp = ScriptList;
	while (temp)
	{
		if ( !strcmp( temp->Event, Event ) && !strcmp( temp->Type, Type ) )
			return temp;
	};
	return NULL;
};

XCXML_Trigger* XCXML_Form::AddTrigger()
{
	XCXML_Trigger* temp = TriggerList;
	XCXML_Trigger* newtrig = new XCXML_Trigger();
	if ( temp )
	{
		while ( temp->next )
			temp = temp->next;
		temp->next = newtrig;
	}
	else
		TriggerList = newtrig;
	return newtrig;
};

XCXML_Trigger* XCXML_Form::GetTrigger( char* Key )
{
	assert( Key );
	XCXML_Trigger* temp = TriggerList;
	while (temp)
	{
		if ( !strcmp( temp->Key, Key ) )
			return temp;
	};
	return NULL;
};

XCXML_Object* XCXML_Form::AddObject()
{
	XCXML_Object* temp = ObjectList;
	XCXML_Object* newobj = new XCXML_Object();
	if ( temp )
	{
		while ( temp->next )
			temp = temp->next;
		temp->next = newobj;
	}
	else
		ObjectList = newobj;
	return newobj;
};

XCXML_Object* XCXML_Form::GetObject( char* Name )
{
	assert( Name );
	XCXML_Object* temp = ObjectList;
	while (temp)
	{
		if ( !strcmp( temp->Name, Name ) )
			return temp;
	};
	return NULL;
};

XCXML_Object* XCXML_Form::GetObject( long Number )
{
	XCXML_Object* temp = ObjectList;
	while (temp)
	{
		if ( atol( temp->Number ) == Number)
			return temp;
	};
	return NULL;
};

// XML Message Tag
//////////////////////////////////////////////////////////////////////////////
XCXML_Message::XCXML_Message()
{
	Number = NULL;
	Severity = NULL;
	Field = NULL;
	Required = NULL;
	Value = NULL;
	Order = NULL;
	next = NULL;
};

XCXML_Message::~XCXML_Message()
{
	if ( Number ) delete[] Number;
	if ( Severity ) delete[] Severity;
	if ( Field ) delete[] Field;
	if ( Required ) delete[] Required;
	if ( Value ) delete[] Value;
};

// XML Field Tag
//////////////////////////////////////////////////////////////////////////////
XCXML_Field::XCXML_Field()
{
	Number = NULL;
	Name = NULL;
	Value = NULL;
	next = NULL;
};

XCXML_Field::~XCXML_Field()
{
	if ( Number ) delete[] Number;
	if ( Name ) delete[] Name;
	if ( Value ) delete[] Value;
};

// XML Buffer Tag
//////////////////////////////////////////////////////////////////////////////
XCXML_Buffer::XCXML_Buffer()
{
	Number = NULL;
	Name = NULL;
	Type = NULL;
	Required = NULL;
	FieldList = NULL;
	next = NULL;
};

XCXML_Buffer::~XCXML_Buffer()
{
	if ( Number ) delete[] Number;
	if ( Name ) delete[] Name;
	if ( Type ) delete[] Type;
	if ( Required ) delete[] Required;
	if ( FieldList )
	{
		XCXML_Field* temp;
		while ( FieldList )
		{
			temp = FieldList;
			FieldList = FieldList->next;
			delete temp;
		};
	};
};

XCXML_Field* XCXML_Buffer::AddField()
{
	XCXML_Field* temp = FieldList;
	XCXML_Field* newfield = new XCXML_Field();
	if ( temp )
	{
		while ( temp->next )
			temp = temp->next;
		temp->next = newfield;
	}
	else
		FieldList = newfield;
	return newfield;
};

XCXML_Field* XCXML_Buffer::GetField( long Number )
{
	XCXML_Field* temp = FieldList;
	while (temp)
	{
		if ( atol( temp->Number ) == Number)
			return temp;
	};
	return NULL;
};

XCXML_Field* XCXML_Buffer::GetField( char* Name )
{
	assert( Name );
	XCXML_Field* temp = FieldList;
	while (temp)
	{
		if ( !strcmp( temp->Name, Name ) )
			return temp;
	};
	return NULL;
};

// XML Transaction Tag
//////////////////////////////////////////////////////////////////////////////
XCXML_Transaction::XCXML_Transaction()
{
	Number = NULL;
	Name = NULL;
	Success = NULL;
	Description = NULL;
	UserName = NULL;
	Password = NULL;
	Super = NULL;
	Action = NULL;
	Phase = NULL;
	Ignore = NULL;
	BufferList = NULL;
	MessageList = NULL;
	next = NULL;
};

XCXML_Transaction::~XCXML_Transaction()
{
	if ( Number ) delete[] Number;
	if ( Name ) delete[] Name;
	if ( Success ) delete[] Success;
	if ( Description ) delete[] Description;
	if ( UserName ) delete[] UserName;
	if ( Password ) delete[] Password;
	if ( Super ) delete[] Super;
	if ( Action ) delete[] Action;
	if ( Phase ) delete[] Phase;
	if ( Ignore ) delete[] Ignore;
	if ( BufferList )
	{
		XCXML_Buffer* btemp;
		while ( BufferList )
		{
			btemp = BufferList;
			BufferList = BufferList->next;
			delete btemp;
		};
	};
	if ( MessageList )
	{
		XCXML_Message* mtemp;
		while ( MessageList )
		{
			mtemp = MessageList;
			MessageList = MessageList->next;
			delete mtemp;
		};
	};
};

XCXML_Buffer* XCXML_Transaction::AddBuffer()
{
	XCXML_Buffer* temp = BufferList;
	XCXML_Buffer* newbuf = new XCXML_Buffer();
	if ( temp )
	{
		while ( temp->next )
			temp = temp->next;
		temp->next = newbuf;
	}
	else
		BufferList = newbuf;
	return newbuf;
};

XCXML_Buffer* XCXML_Transaction::GetBuffer( long Number )
{
	XCXML_Buffer* temp = BufferList;
	while (temp)
	{
		if ( atol( temp->Number ) == Number)
			return temp;
	};
	return NULL;
};

XCXML_Buffer* XCXML_Transaction::GetBuffer( char* Name )
{
	assert( Name );
	XCXML_Buffer* temp = BufferList;
	while (temp)
	{
		if ( !strcmp( temp->Name, Name ) )
			return temp;
	};
	return NULL;
};

XCXML_Message* XCXML_Transaction::AddMessage()
{
	XCXML_Message* temp = MessageList;
	XCXML_Message* newmess = new XCXML_Message();
	if ( temp )
	{
		while ( temp->next )
			temp = temp->next;
		temp->next = newmess;
	}
	else
		MessageList = newmess;
	return newmess;
};

XCXML_Message* XCXML_Transaction::GetMessage( long Number )
{
	XCXML_Message* temp = MessageList;
	while (temp)
	{
		if ( atol( temp->Number ) == Number)
			return temp;
	};
	return NULL;
};

// XML TransactionList Tag
//////////////////////////////////////////////////////////////////////////////
XCXML_TransactionList::XCXML_TransactionList()
{
	ApplNumber = NULL;
	Type = NULL;
	UserName = NULL;
	Password = NULL;
	Transactions = NULL;
	next = NULL;
};

XCXML_TransactionList::~XCXML_TransactionList()
{
	if ( ApplNumber ) delete[] ApplNumber;
	if ( Type ) delete[] Type;
	if ( UserName ) delete[] UserName;
	if ( Password ) delete[] Password;
	if ( Transactions )
	{
		XCXML_Transaction* temp;
		while ( Transactions )
		{
			temp = Transactions;
			Transactions = Transactions->next;
			delete temp;
		};
	};
};
	
XCXML_Transaction* XCXML_TransactionList::AddTransaction()
{
	XCXML_Transaction* temp = Transactions;
	XCXML_Transaction* newtrans = new XCXML_Transaction();
	if ( temp )
	{
		while ( temp->next )
			temp = temp->next;
		temp->next = newtrans;
	}
	else
		Transactions = newtrans;
	return newtrans;
};

// XML Protea (root node)
//////////////////////////////////////////////////////////////////////////////
XCXML_Protea::XCXML_Protea()
{
	Version = NULL;
	ApplNumber = NULL;
	TermType = NULL;
	FormList = NULL;
	TransactionListList = NULL;
};

XCXML_Protea::~XCXML_Protea()
{
	if ( Version ) delete[] Version;
	if ( ApplNumber ) delete[] ApplNumber;
	if ( TermType ) delete[] TermType;
	if ( FormList )
	{
		XCXML_Form* ftemp;
		while (FormList)
		{
			ftemp = FormList;
			FormList = FormList->next;
			delete ftemp;
		};
	};
	if ( TransactionListList )
	{
		XCXML_TransactionList* ttemp;
		while ( TransactionListList )
		{
			ttemp = TransactionListList;
			TransactionListList = TransactionListList->next;
			delete ttemp;
		};
	};
};

XCXML_Form* XCXML_Protea::AddForm()
{
	XCXML_Form* temp = FormList;
	XCXML_Form* newform = new XCXML_Form();
	if ( temp )
	{
		while ( temp->next )
			temp = temp->next;
		temp->next = newform;
	}
	else
		FormList = newform;
	return newform;
};

XCXML_Form* XCXML_Protea::GetForm( char* Name )
{
	assert( Name );
	XCXML_Form* temp = FormList;
	while (temp)
	{
		if ( !strcmp( temp->Name, Name ) )
			return temp;
	};
	return NULL;
};

XCXML_Form* XCXML_Protea::GetForm( long Number )
{
	XCXML_Form* temp = FormList;
	while (temp)
	{
		if ( atol( temp->Number ) == Number)
			return temp;
	};
	return NULL;
};

XCXML_TransactionList* XCXML_Protea::AddTransactionList()
{
	XCXML_TransactionList* temp = TransactionListList;
	XCXML_TransactionList* newtlist = new XCXML_TransactionList();
	if ( temp )
	{
		while ( temp->next )
			temp = temp->next;
		temp->next = newtlist;
	}
	else
		TransactionListList = newtlist;
	return newtlist;
};
